package com.bawei.service.impl;

import com.bawei.mapper.ExamineMapper;
import com.bawei.mapper.NoticeMapper;
import com.bawei.mapper.RecipinetNoticeMapper;
import com.bawei.mapper.UserInfoMapper;
import com.bawei.pojo.*;
import com.bawei.service.NoticeService;
import com.bawei.util.PageView;
import com.bawei.util.SpringMvcFileDownload;
import com.bawei.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private NoticeMapper  noticeMapper;

    @Autowired
    private RecipinetNoticeMapper recipinetNoticeMapper;


    @Autowired
    private ExamineMapper examineMapper;


    @Override
    public List<UserInfo> selectUsers() {
        return userInfoMapper.selectByExample(null);
    }

    @Override
    public void save(Notice notice, String[] recipinet, MultipartFile[] file, UserInfo userInfo, HttpServletRequest request) {
        if(file!=null && file.length!=0){
            notice.setIsAccessory(1);  //有文件
            for (MultipartFile multipartFile : file) {
                File file1 = SpringMvcFileDownload.upload(multipartFile, request);
                String path = file1.getPath();
                System.out.println(path+"上传图片的路径");
            }
        }else{
            notice.setIsAccessory(0);
        }
        for (String s : recipinet) {
            notice.setId(StringUtil.uuid());
            notice.setUserId(userInfo.getId());
            notice.setStatus(0);
            notice.setPublishTime(new Date());
            noticeMapper.insertSelective(notice);
            RecipinetNotice recipinetNotice = new RecipinetNotice();
            recipinetNotice.setNoticeId(notice.getId());
            recipinetNotice.setRecipinetId(s);
            recipinetNoticeMapper.insertSelective(recipinetNotice);
            Examine examine=new Examine();
            examine.settType("公告管理");
            examine.setId(StringUtil.uuid());
            examine.settId(notice.getId());
            examine.setStatus(0);
            examineMapper.insertSelective(examine);
        }
    }

    @Override
    public PageView<Notice> myPublish(String id, Integer currentPage) {
        PageHelper.startPage(currentPage, 3);
        NoticeExample example=new NoticeExample();
        NoticeExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<Notice> notices = noticeMapper.selectByExample(example);
        PageInfo<Notice> pageInfo = new PageInfo<Notice>(notices);
        PageView<Notice> noticePageView = new PageView<>();
        noticePageView.setTotalPage(pageInfo.getTotal());
        noticePageView.setTotalRecord((long) notices.size());
        noticePageView.setRecords(notices);
        return noticePageView;
    }
}
